package UILayer;

import java.awt.EventQueue;

import javax.swing.JTable;
import javax.swing.ListSelectionModel;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;

import java.awt.BorderLayout;
import java.awt.Point;
import java.util.ArrayList;

import javax.swing.JPanel;

import java.awt.Dimension;

import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;

import java.awt.FlowLayout;

import javax.swing.JLabel;
import javax.swing.JTextField;

import CtrLayer.OrderCtr;
import CtrLayer.StockCtr;

import javax.swing.JInternalFrame;
import javax.swing.JButton;
import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.UIManager;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.swing.JTextArea;

public class OrderDetails extends JInternalFrame
{
	private JTable product_table;
	private String[] column_names = { "Art. number", "Name", "Amount", "Total cost" };
	//JPanel panel;
	private JTextField tot_price_tf;
	private OrderCtr order_ctr;
	private StockCtr stock_ctr;
	private String customer_name;
	private String order_id;
	private JTextField spec_dis_tf;
	private JTextArea del_note_ta;
	private JTextArea add_inf_ta;
	private JTextArea del_term_ta;

	/**
	 * Create the frame.
	 */
	public OrderDetails(String i_customer_name, String i_order_id)
	{
		super("Sale details", true, true, true);
		
		customer_name = i_customer_name;
		order_id = i_order_id;
		
		setBounds(100, 100, 644, 477);
		getContentPane().setLayout(new BorderLayout(0, 0));
		order_ctr = new OrderCtr();
		stock_ctr = new StockCtr();
		
		make_table();
		make_btn();
		make_fields();
		make_top();
		

	}
	
	private void make_btn()
	{
		JPanel panel_1 = new JPanel();
		getContentPane().add(panel_1, BorderLayout.SOUTH);
		
		JButton ok_btn = new JButton("Ok");
		ok_btn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { dispose(); } });
		panel_1.add(ok_btn);
	}
	
	private void make_top()
	{
		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.NORTH);
		panel.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_cust = new JPanel();
		panel.add(panel_cust, BorderLayout.WEST);
		panel_cust.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		JLabel lblNewLabel = new JLabel("Customer");
		panel_cust.add(lblNewLabel);
		
		JTextField cust_name = new JTextField();
		cust_name.setColumns(20);
		cust_name.setEnabled(false);
		cust_name.setText(customer_name);
		panel_cust.add(cust_name);
		
		JPanel panel_2 = new JPanel();
		panel.add(panel_2, BorderLayout.EAST);
		panel_2.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		JLabel tot_price_lbl = new JLabel("Total price");
		panel_2.add(tot_price_lbl);

		tot_price_tf = new JTextField();
		tot_price_tf.setEnabled(false);
		tot_price_tf.setText(String.valueOf(order_ctr.provide_total_cost_of_order(order_id)));;
		panel_2.add(tot_price_tf);
		tot_price_tf.setColumns(10);
		
		
	}
	
	private void make_fields()
	{
		Object[] fields = order_ctr.get_order_fields(order_id);
		
		JPanel panel_1 = new JPanel();
		getContentPane().add(panel_1, BorderLayout.EAST);
		GridBagLayout gbl_panel_1 = new GridBagLayout();
		gbl_panel_1.columnWidths = new int[]{0, 144};
		gbl_panel_1.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		gbl_panel_1.columnWeights = new double[]{1.0, 1.0};
		gbl_panel_1.rowWeights = new double[]{0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		panel_1.setBorder(new EmptyBorder(5, 2, 2, 7));
		panel_1.setLayout(gbl_panel_1);
		
		JCheckBox paid_checkbox = new JCheckBox("Paid");
		paid_checkbox.setEnabled(false);
		paid_checkbox.setSelected((boolean) fields[5]);
		GridBagConstraints gbc_paid_checkbox = new GridBagConstraints();
		gbc_paid_checkbox.anchor = GridBagConstraints.WEST;
		gbc_paid_checkbox.insets = new Insets(0, 0, 5, 5);
		gbc_paid_checkbox.gridx = 0;
		gbc_paid_checkbox.gridy = 0;
		panel_1.add(paid_checkbox, gbc_paid_checkbox);
		
		JLabel lblSpecialDiscount = new JLabel("Special discount");
		GridBagConstraints gbc_lblSpecialDiscount = new GridBagConstraints();
		gbc_lblSpecialDiscount.anchor = GridBagConstraints.WEST;
		gbc_lblSpecialDiscount.insets = new Insets(0, 0, 5, 5);
		gbc_lblSpecialDiscount.gridx = 0;
		gbc_lblSpecialDiscount.gridy = 1;
		panel_1.add(lblSpecialDiscount, gbc_lblSpecialDiscount);
		
		spec_dis_tf = new JTextField();
		spec_dis_tf.setEnabled(false);
		spec_dis_tf.setText(String.valueOf(fields[4]));
		GridBagConstraints gbc_spec_dis_tf = new GridBagConstraints();
		gbc_spec_dis_tf.insets = new Insets(0, 0, 5, 5);
		gbc_spec_dis_tf.fill = GridBagConstraints.HORIZONTAL;
		gbc_spec_dis_tf.gridx = 1;
		gbc_spec_dis_tf.gridy = 1;
		panel_1.add(spec_dis_tf, gbc_spec_dis_tf);
		spec_dis_tf.setColumns(10);
		
		JLabel label = new JLabel("%");
		GridBagConstraints gbc_label = new GridBagConstraints();
		gbc_label.insets = new Insets(0, 0, 5, 0);
		gbc_label.gridx = 2;
		gbc_label.gridy = 1;
		panel_1.add(label, gbc_label);
		
		JLabel lblAdditionalInformation = new JLabel("Additional information");
		GridBagConstraints gbc_lblAdditionalInformation = new GridBagConstraints();
		gbc_lblAdditionalInformation.anchor = GridBagConstraints.EAST;
		gbc_lblAdditionalInformation.insets = new Insets(0, 0, 5, 5);
		gbc_lblAdditionalInformation.gridx = 0;
		gbc_lblAdditionalInformation.gridy = 2;
		panel_1.add(lblAdditionalInformation, gbc_lblAdditionalInformation);
		
		
		
		JLabel lblDeliveryTerms = new JLabel("Delivery terms");
		GridBagConstraints gbc_lblDeliveryTerms = new GridBagConstraints();
		gbc_lblDeliveryTerms.anchor = GridBagConstraints.WEST;
		gbc_lblDeliveryTerms.insets = new Insets(0, 0, 5, 5);
		gbc_lblDeliveryTerms.gridx = 0;
		gbc_lblDeliveryTerms.gridy = 3;
		panel_1.add(lblDeliveryTerms, gbc_lblDeliveryTerms);
		
		
		
		JLabel lblDeliveryNote = new JLabel("Delivery note");
		GridBagConstraints gbc_lblDeliveryNote = new GridBagConstraints();
		gbc_lblDeliveryNote.anchor = GridBagConstraints.WEST;
		gbc_lblDeliveryNote.insets = new Insets(0, 0, 5, 5);
		gbc_lblDeliveryNote.gridx = 0;
		gbc_lblDeliveryNote.gridy = 4;
		panel_1.add(lblDeliveryNote, gbc_lblDeliveryNote);
		
		JScrollPane scrollPane_2 = new JScrollPane();
		GridBagConstraints gbc_scrollPane_2 = new GridBagConstraints();
		gbc_scrollPane_2.insets = new Insets(0, 0, 5, 5);
		gbc_scrollPane_2.fill = GridBagConstraints.BOTH;
		gbc_scrollPane_2.gridx = 1;
		gbc_scrollPane_2.gridy = 4;
		panel_1.add(scrollPane_2, gbc_scrollPane_2);
		
		//JTextArea textArea_2 = new JTextArea();
		
		JScrollPane scrollPane = new JScrollPane();
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.insets = new Insets(0, 0, 5, 5);
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridx = 1;
		gbc_scrollPane.gridy = 2;
		panel_1.add(scrollPane, gbc_scrollPane);
		
		add_inf_ta = new JTextArea()
		{
	      public void setEnabled(boolean enabled)
	      {
	        super.setEnabled(enabled);
	        if(enabled == false)
	        {
	          JTextField tf = new JTextField();
	          tf.setEditable(false);
	          setBackground(tf.getBackground());
	        }
	        else setBackground(UIManager.getColor("TextArea.background"));
	      }
	    };
	    add_inf_ta.setText((String) fields[3]);
	    add_inf_ta.setEnabled(false);
		scrollPane.setViewportView(add_inf_ta);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		GridBagConstraints gbc_scrollPane_1 = new GridBagConstraints();
		gbc_scrollPane_1.insets = new Insets(0, 0, 5, 5);
		gbc_scrollPane_1.fill = GridBagConstraints.BOTH;
		gbc_scrollPane_1.gridx = 1;
		gbc_scrollPane_1.gridy = 3;
		panel_1.add(scrollPane_1, gbc_scrollPane_1);
		
		del_term_ta = new JTextArea()
		{
	      public void setEnabled(boolean enabled)
	      {
	        super.setEnabled(enabled);
	        if(enabled == false)
	        {
	          JTextField tf = new JTextField();
	          tf.setEditable(false);
	          setBackground(tf.getBackground());
	        }
	        else setBackground(UIManager.getColor("TextArea.background"));
	      }
	    };
		del_term_ta.setText((String) fields[2]);
		del_term_ta.setEnabled(false);
		scrollPane_1.setViewportView(del_term_ta);
		
		del_note_ta = new JTextArea()
		{
	      public void setEnabled(boolean enabled)
	      {
	        super.setEnabled(enabled);
	        if(enabled == false)
	        {
	          JTextField tf = new JTextField();
	          tf.setEditable(false);
	          setBackground(tf.getBackground());
	        }
	        else setBackground(UIManager.getColor("TextArea.background"));
	      }
	    };
		    
		    del_note_ta.setEnabled(false);
		    del_note_ta.setText((String) fields[1]);
			scrollPane_2.setViewportView(del_note_ta);
		
		JCheckBox with_del_checkbox = new JCheckBox("Delivery");
		with_del_checkbox.setEnabled(false);
		with_del_checkbox.setSelected((boolean)fields[0]);
		GridBagConstraints gbc_with_del_checkbox = new GridBagConstraints();
		gbc_with_del_checkbox.anchor = GridBagConstraints.WEST;
		gbc_with_del_checkbox.insets = new Insets(0, 0, 5, 5);
		gbc_with_del_checkbox.gridx = 0;
		gbc_with_del_checkbox.gridy = 5;
		panel_1.add(with_del_checkbox, gbc_with_del_checkbox);
	}
	
	private void make_table()
	{



		product_table = new JTable();
		product_table.setFillsViewportHeight(true);
		fill_table();

		product_table.setPreferredScrollableViewportSize(new Dimension(500, 70));
		product_table.setAutoCreateRowSorter(true);
		product_table.setRowSelectionAllowed(true);
		product_table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		product_table.getTableHeader().setReorderingAllowed(false);
		product_table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

		product_table.addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseClicked(MouseEvent e)
			{
				if (e.getClickCount() == 2)
				{
					details();
				}
			}
		});

		JScrollPane scroll_pane = new JScrollPane(product_table);
		scroll_pane.setEnabled(false);
		scroll_pane.setBorder(new EmptyBorder(5, 10, 5, 10));
		getContentPane().add(scroll_pane, BorderLayout.CENTER);
	}
	
	private void fill_table()
	{
		ArrayList<String> ids = order_ctr.get_ids(order_id);
		ArrayList<String> amounts = order_ctr.get_amounts(order_id);
		Object[][] filling = new Object[ids.size()][4];
		for (int i = 0; i < ids.size(); i++)
		{
			String art_nr = ids.get(i);
			int amount = Integer.parseInt(amounts.get(i));
			Object[] data = { art_nr, amount };
			String name = stock_ctr.get_name_via_art_nr(art_nr);
			double total_cost = stock_ctr.get_sales_line_item_cost(data);
			Object[] temp = { Integer.parseInt(art_nr), name, amount, total_cost };
			filling[i] = temp;
		}
		product_table.setModel(new MyTableModel(filling, column_names));
	}
	
	
	private void details()
	{
		int row_index = product_table.getSelectedRow();
		if (row_index != -1)
		{
			if( product_table.getModel().getValueAt(row_index, 0) != null)
			{
				ProductManager p = new ProductManager(
						String.valueOf( product_table.getModel().getValueAt(row_index, 0)), true, false);
				getDesktopPane().add(p);
				Point current_win = getLocationOnScreen();
				int x = (int) current_win.getX() + 20;
				int y = (int) current_win.getY() + 5;
				p.setLocation(x, y);
				p.setVisible(true);
			}
		}
	}

}
